﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using prototype123.models;
using prototype123.controllers;

namespace prototype123.data_access
{
    class UserDAO
    {
        private SqlConnection db_connection;

        public void Connect()
        {

            db_connection = new SqlConnection(SystemController.connectString);
            db_connection.Open();
        }

        public void Disconnect()
        {
            db_connection.Close();
        }

        //Given a name and password, Load checks the db to see if the username and password match. 
        //if they match, it returns a User object. If they do not match, it returns null
        public User Load(string name, string pass)
        {
            User usr = new User();
            SqlCommand password;
            string passwordResult;

            try
            {
                password = new SqlCommand("SELECT PASSWORD FROM USERS WHERE USERNAME = '" + name + "'", db_connection);
                SqlDataReader reader = password.ExecuteReader();
                reader.Read();
                passwordResult = reader[0].ToString();
                reader.Close();
            }
            catch { return null; }

            usr.Username = name;
            usr.Password = passwordResult;
            return usr;
        }
    }
}
